package com.test.nnooolwdeaer.dialog_wooden;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseApplication_wooden;
import com.test.nnooolwdeaer.bean_wooden.config_info_wooden.AppShowInfo_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;

import java.util.Objects;

public class UpgradeDialog_wooden extends Dialog {

    private final boolean isForce;
    private final Runnable onCloseRunnable;

    public UpgradeDialog_wooden(@NonNull Context context, boolean isForce, Runnable onCloseRunnable) {
        super(context);
        this.isForce = isForce;
        this.onCloseRunnable = onCloseRunnable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_upgrade_wooden);
        initView_wooden();
    }

    @SuppressLint("SetTextI18n")
    private void initView_wooden(){

        try {
            Objects.requireNonNull(getWindow()).setBackgroundDrawableResource(R.color.trans);
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            setCancelable(false);
        }catch (Exception e){
            if (Constant_wooden.Debug) e.printStackTrace();
        }

        TextView versionTv = findViewById(R.id.upgrade_tv_version);
        TextView descTv = findViewById(R.id.upgrade_tv_desc);
        Button upgradeBtn = findViewById(R.id.upgrade_btn_upgrade);
        TextView tipsTv = findViewById(R.id.upgrade_tv_tips);
        ImageButton closeIb = findViewById(R.id.upgrade_ib_close);

        if (isForce){
            tipsTv.setVisibility(GONE);
            closeIb.setVisibility(GONE);
            descTv.setText(getContext().getString(R.string.upgrade_force_desc, BaseApplication_wooden.getApplication().versionName()));
        }else {
            tipsTv.setVisibility(VISIBLE);
            closeIb.setVisibility(VISIBLE);

            String desc = AppShowInfo_wooden.getInstance().lastVersionDesc();
            if (!TextUtils.isEmpty(desc)) descTv.setText(desc);
            else descTv.setText(getContext().getString(R.string.upgrade_default_desc));

            closeIb.setOnClickListener(view -> {
                HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("DIALOG_UPGRADE_CLOSE_CLICK");
                dismiss();
                if (onCloseRunnable!=null) onCloseRunnable.run();
            });
        }

        versionTv.setText(AppShowInfo_wooden.getInstance().lastVersionName());

        upgradeBtn.setOnClickListener(view -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("DIALOG_UPGRADE_UPGRADE_NOW_CLICK");
            getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + BaseApplication_wooden.getApplication().getPackageName())));
        });

    }

}
